package com.island.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.DefaultFileItemFactory;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.island.model.Content;
import com.island.service.ContentService;
import com.island.util.WebsiteConfig;


@Controller
@RequestMapping("/content")
public class ContentController {
	
	@Autowired
	private ContentService contentService;
	
	
	/**
	 * 查询所有内容
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findAllContent")
	public String findAllContent(HttpServletRequest request,
			HttpServletResponse response) {
		//获取用户
		List<Content> contentList = contentService.findAllContent();
		System.out.println(contentList.size());
		request.setAttribute("contentList", contentList);
		//加日志
		return "content/list";
	}
	
	/**
	 * 添加内容
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addContent")
	public String crreateContent(HttpServletRequest request,
			HttpServletResponse response){
		//获取用户
		
		String title = request.getParameter("titile");
		String subTitle = request.getParameter("sub_title");
		String author = request.getParameter("author");
		String keyword = request.getParameter("keyword");
		String docket = request.getParameter("docket");
		String contentText = request.getParameter("content_text");
		String classid = request.getParameter("classid");
		String pubUserId = request.getParameter("userid");
		String pubTime = String.valueOf(System.currentTimeMillis());
		String pubStatus = WebsiteConfig.ContentPublish_Status_Edit;
		
		Content content = new Content();
		content.setTitle(title);
		content.setSub_title(subTitle);
		content.setAuthor(author);
		content.setKeyword(keyword);
		content.setDocket(docket);
		content.setContent_text(contentText);
		content.setClassid(classid);
		content.setPub_user_id(pubUserId);
		content.setPub_time(pubTime);
		content.setPub_status(pubStatus);
		
		contentService.createContent(content);
		
		//加日志
		return findAllContent(request,response);
	}
	
	/**
	 * 通过ID查询内容
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findContentByID")
	public String findContentByID(HttpServletRequest request,
			HttpServletResponse response){
		
		//获取用户
		
		String contentID = request.getParameter("contentID");
		Content content = contentService.findContentByID(contentID);
		//加日志
		request.setAttribute("content", content);
		return "content/edit";
	}
	
	/**
	 * 修改内容
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateContent")
	public String updateContent(HttpServletRequest request,
			HttpServletResponse response){
		//获取用户

		String contentID = request.getParameter("contentID");
		
		String title = request.getParameter("titile");
		String subTitle = request.getParameter("sub_title");
		String author = request.getParameter("author");
		String keyword = request.getParameter("keyword");
		String docket = request.getParameter("docket");
		String contentText = request.getParameter("content_text");
		String classid = request.getParameter("classid");
		String pubUserId = request.getParameter("userid");
		
		Content content = contentService.findContentByID(contentID);
		
		content.setTitle(title);
		content.setSub_title(subTitle);
		content.setAuthor(author);
		content.setKeyword(keyword);
		content.setDocket(docket);
		content.setContent_text(contentText);
		content.setClassid(classid);
		content.setPub_user_id(pubUserId);
		
		contentService.updateContent(content);
		
		//加日志
		return findAllContent(request,response);
	}
	/**
	 * 根据ID删除内容
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/deleteContent")
	public String deleteContent(HttpServletRequest request,
			HttpServletResponse response) {
		//获取用户

		String contentID = request.getParameter("contentID");
		if(contentID!=null&&!("").equals(contentID)){
			contentService.deleteContent(Integer.parseInt(contentID));
		}
		//加日志
		return findAllContent(request,response);
	}
	
	/**
	 * 上传视频到WebRoot/video下
	 * @param request
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/videoUpload")
	public String videoUpload(HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("GBK");
		FileItemFactory factory = new DefaultFileItemFactory();
		FileUpload upload = new FileUpload(factory);
		
		Iterator items;
		try {
			items = upload.parseRequest(request).iterator();
			while(items.hasNext()){
				FileItem item = (FileItem)items.next();
				if(!item.isFormField()){
					String contentID = request.getParameter("contentID");
					String name = item.getName();
					String fileType = name.substring(name.lastIndexOf("."));
					String fileName = contentID+fileType;
					Content content = contentService.findContentByID(contentID);
					if(content != null){
						content.setVideo_name(fileName);
						contentService.updateContent(content);
					}
					System.out.println(fileName+"-----"+new String(name.getBytes("GBK"),"UTF-8"));
					String realPath = request.getRealPath("/");
					System.out.println(realPath+"-----realPath");
					String path = realPath+File.separatorChar+WebsiteConfig.Content_News_Video+fileName;
					File file = new File(path);
					if(file.exists()){
						System.out.println("保存的地址为："+path);
					}else{
						System.out.println(file.getPath()+"路径不存在");
						file.createNewFile();
					}
					item.write(file);
					response.setContentType("text/html");
					response.setCharacterEncoding("GBK");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
